package cn.clq.user.service.impl;

import cn.clq.basic.constant.Constant;
import cn.clq.basic.dto.RegisterDto;
import cn.clq.basic.exception.BusinessException;
import cn.clq.basic.util.MD5Utils;
import cn.clq.basic.util.StrUtils;
import cn.clq.user.domain.Logininfo;
import cn.clq.user.domain.User;
import cn.clq.user.mapper.LogininfoMapper;
import cn.clq.user.mapper.UserMapper;
import cn.clq.user.service.IUserService;
import cn.clq.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenlei
 * @since 2022-06-17
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * @description:手机注册
     * @param: dto
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/17 23:29
     */
    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordConfirm = dto.getPasswordConfirm();
        //1.校验两次密码是否一致
        if(!password.equals(passwordConfirm)){
            //不一致报错，一致往下执行
            throw new BusinessException("两次输入密码不一致，请重新输入！！！");
        }
        //2.校验短信验证码
        //从Redis中拿验证码
        String smsCodeTmp = (String) redisTemplate.opsForValue().get(phone + Constant.SmsCode.REGISTER_SUFFIX);
        //2.1拿不到，说明短信验证码已经失效，需要重新获取短信验证码
        if(smsCodeTmp==null){
            throw new BusinessException("短信验证码已失效，请重新获取");
        }
        //2.2拿到了，进行比较
        if(!smsCode.equals(smsCodeTmp.split(":")[0])){
            throw new BusinessException("短信验证码错误，请核对后重新输入");
        }
        //3.保存用户数据,需要级联修改t_user + t_logininfo两个表
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    //将dto转换成user对象，然后加密保存
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        //获得盐值
        String salt = StrUtils.getComplexRandomString(32);
        //对密码进行加密加盐
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }
    //同步修改t_logininfo表
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //拷贝同名字段的值(old,new)
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(Constant.PersionType.USER);//手动设置用户类型
        return logininfo;
    }
}
